<template>
  <div class="login-form">
    <el-form :model="ruleForm3" :rules="rules3" ref="ruleForm3" label-width="100px" class="ruleForm3">
      <el-form-item label="密码" required prop="pass">
        <el-input type="password" placeholder="密码" v-model="ruleForm3.pass" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" required prop="checkPass">
        <el-input type="password" placeholder="确认密码" v-model="ruleForm3.checkPass" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" style="width: 100%" @click="submitForm('ruleForm3')">完成注册</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import md5 from 'utils/md5'
  export default {
    name: "part3",
    data() {
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          return callback(new Error('请输入密码'));
        } else if (!/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,12}$/.test(value)) {
          return callback(new Error('密码长度6-12位数字+字母组合'));
        } else {
          if (this.ruleForm3.checkPass !== '') {
            this.$refs.ruleForm3.validateField('checkPass');
          }
          return callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          return callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm3.pass) {
          return callback(new Error('两次输入密码不一致!'));
        } else {
          return callback();
        }
      };
      return ({
        ruleForm3: {
          pass: '',
          checkPass: '',
        },
        rules3: {
          pass: [
            { validator: validatePass, trigger: 'blur' }
          ],
          checkPass: [
            { validator: validatePass2, trigger: 'blur' }
          ]
        }
      })
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.registerEnterprise()
          } else {
            return false;
          }
        });
      },
      registerEnterprise() {
        console.log(this.$store.state.signForm)
        const formData = this.$store.state.signForm
        const params = _.pick(
          formData,
          ['phone', 'uuid', 'imgCode', 'randomCode', 'companyName', 'brandName', 'staffSize', 'userName', 'email']
        )
        this.$http.post(this.GLOBAL.host, {
          method: "User_Company",
          data: {
            ...params,
            password: md5(this.ruleForm3.pass),
            industryId: _.last(formData.industryIds),
            province: formData.regionIds[0],
            city: formData.regionIds[1],
            area: formData.regionIds[2],
          }
        }).then(res => {
          if (res.data.code === 200) {
            this.$message({
              message: '注册成功',
              type: 'success'
            });
            this.$store.commit('resetSignForm')
            this.$router.push({path: '/login'});
          } else {
            this.$message({
              message: res.data.data,
              type: 'error'
            });
          }
        })
      }
    }
  }
</script>

<style scoped>

</style>
